import Common from '../common'
var Js = Common('order')
export default {
  name: 'order',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'order_no',
          label: '订单NO',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 150,
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入订单NO',
            clearable: true,
            defaultValue: '',
            optionsName: '',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.order_no = ['LIKE', ['%' + val + '%']];
              } else {
                delete self.searchItem.order_no;
              };
            },
          },
        },
        {
          key: 'price',
          label: '订单总金额',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 120
        },
        {
          key: 'balance',
          label: '奖励金抵扣',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 120
        },
        {
          key: 'count',
          label: '数量',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 100
        },
        {
          key: "pay_type",
          label: '支付方式',
          listType: 'normal',
          formatter: function(val, tests) {
            return [ '微信支付', '货到付款', '银行卡转账','二维码收款'][val.pay_type];
          },
          componentName: 'sls-select',
        },
        {
          key: "pay_status",
          label: '支付状态',
          application: ['编辑'],
          type: 'select',
          listType: 'normal',
          formatter: function(val, tests) {
            return [ '退款', '否', '是'][val.pay_status+1];
          },
          componentName: 'sls-select',
          optionsName: 'payOptions',
          filter_multiple: false,
          listType: 'normal',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          placeholder: '请选择是否支付',
          width: 80,
        },
        {
          key: "transport_status",
          label: '配送状态',
          application: ['编辑'],
          listType: 'normal',
          formatter: function(val, tests) {
            return [ '待配送', '待收货', '已完成'][val.transport_status];
          },
          componentName: 'sls-select',
          optionsName: 'transportOptions',
          filter_multiple: false,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
        },
        {
          key: "order_step",
          label: '订单状态',
          application: [],
          type: 'select',
          listType: 'normal',
          formatter: function(val, tests) {
            if (val.order_step == -1) {
              return '拒绝撤单'
            } else {
              return ['正常下单', '申请撤单', '已撤单', '完结'][val.order_step];
            }
          },
          componentName: 'sls-select',
          optionsName: 'stepOptions',
          filter_multiple: false,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
        },
        {
          key: 'passage1',
          label: '推广业务员',
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val){
            return val.staff.login_name?val.staff.login_name+'【'+val.passage1+'】':val.passage1
          }
        },
        {
          key: "express_company",
          label: '快递公司',
          application: ['编辑'],
          componentName: 'sls-input',
          // componentName: 'sls-select',
          // optionsName: 'expressOptions',
          // defaultProps: {
          //   label: 'title',
          //   value: 'title',
          // }
        },
        {
          key: 'express_no',
          label: '快递单号',
          application: ['编辑'],
          componentName: 'sls-input',
        },
        {
          key: 'user_no',
          label: '用户NO',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'name',
          label: '用户昵称',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val, tests) {
            return val.user.nickname ? val.user.nickname : '';
          },
        },
        {
          key: 'remark',
          label: '备注',
          application: ['编辑'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            defaultValue: '',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };

            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 200
        },
      ],

      // 按钮配置
      btn_info: [{
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑'
          },
          func: {
            apiName: function(self) {
              return "api_orderUpdate"
            },
            formData: function(self) {
              return self.formData;
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                  user_type: 0,
                },
                data: self.submitData,
                saveAfter: [{
                  tableName: 'Order',
                  FuncName: 'update',
                  data: self.submitData,
                  searchItem: {
                    parent_no: self.formData.order_no
                  }
                }]
              };
              return postData;
            }
          },
        },
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {
            apiName: function(self) {
              return "api_orderUpdate"
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                  user_type: 0,
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          func: {
            func: function(self) {
              self.excelOutput(self);
            },
          },
          text: function(data) {
            return '导出'
          },
          funcType: 'func',
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          funcType: 'func',
          position: 'list',
          text: function(data) {
            return '退款'
          },
          styleFunc: function(data) {
            if (data.order_step == 1 && data.apply_type == 1) {
              return 'background-color: #2c9a7f;border-color:#2c9a7f'
            } else {
              return 'display: none'
            }
          },
          func: {
            formData: function(self) {
              return self.formData;
            },
            func: function(self) {
              self.refundOrder(self.formData);
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          funcType: 'submit',
          text: function(data) {
            return '拒绝'
          },
          styleFunc: function(data) {
            if (data.order_step == 1 && data.apply_type == 1) {
              return 'background-color: #F56C6C;border-color:#F56C6C'
            } else {
              return 'display: none'
            }
          },
          func: {
            apiName: function(self) {
              return "api_orderUpdate"
            },
            formData: function(self) {
              return self.formData;
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  order_no: self.formData.order_no
                },
                data: {
                  order_step: -1
                }
              };
              postData.saveAfter = [{
                tableName: 'Order',
                FuncName: 'update',
                data: {
                  order_step: -1
                },
                searchItem: {
                  parent_no: self.formData.order_no
                }
              }]
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          noAuth: true,
          text: function(data) {
            return '返回'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              var fullPath = self.$route.fullPath;
              var tabList = self.$store.state.tabs.tabs;
              var routerRes = self.$$getBackRoute(tabList, fullPath);
              self.$store.commit('REMOVE_TABS', {
                fullPath: fullPath,
              });
              if (routerRes.backRes) {
                self.$router.push(routerRes.backRes);
              } else {
                self.$router.push(routerRes.newRes);
              };
            },
          },
        },
      ],

      searchItem: {
        level: 0,
        order_type: 1,
        pay_status: 1
      },

      startSearchItem: {
        level: 0,
        order_type: 1,
        pay_status: 1
      },
      optionData: {
        typeOptions: [{
            text: '快递',
            value: 1
          },
          {
            text: '自提',
            value: 2
          },
        ],
        payOptions: [{
          text: '未支付',
          value: 0
        }, {
          text: '已支付',
          value: 1
        }, {
          text: '退款',
          value: -1
        }],
        transportOptions: [{
          text: '待配送',
          value: 0
        }, {
          text: '待收货',
          value: 1
        }, {
          text: '已完成',
          value: 2
        }],
        stepOptions: [{
          text: '正常下单',
          value: 0
        }, {
          text: '申请撤单',
          value: 1
        }, {
          text: '已撤单',
          value: 2
        }, {
          text: '完结',
          value: 3
        }],
        behaviorOptions: [{
          text: '采购',
          value: 1
        }, {
          text: '仓储',
          value: 2
        }],
        labelOptions: [],
        expressOptions: []
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },
      Shop: {
        tableName: 'Shop',
        searchItem: {},
        key: 'shop_no',
        middleKey: 'shop_no',
        condition: 'in',
      },

    }

  },

  mounted() {
    const self = this;
    self.table_arguments.tree_props = {
      children: 'child',
      child: 'child',
      hasChildren: 'child'
    }
    self.table_arguments.expand = true;
    if (this.$store.state.user.userinfo.role != 3) {
      self.searchItem.shop_no = this.$store.state.user.userinfo.user_no;
      self.startSearchItem.shop_no = this.$store.state.user.userinfo.user_no;
    }
    self.user_no = this.$route.params.user_no;
    self.type = this.$route.params.type;
    self.backPath = this.$route.params.backPath;
    if(self.type==1){
      self.searchItem.passage1 = self.user_no;
      self.startSearchItem.passage1 = self.user_no;
    }else{
      self.parent_no = this.$route.params.parent_no;
      self.searchItem.user_no = self.user_no;
      self.startSearchItem.user_no = self.user_no;
      self.searchItem.passage1 = self.parent_no;
      self.startSearchItem.passage1 = self.parent_no;
    }
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();
      this.initLabel();
      this.initExpress();
    },


    async excelOutput(self) {

      if (self.btnName == '导出') {
        const postData = {};
        postData.token = self.$store.getters.getToken;
        if (self.searchItem) {
          postData.searchItem = self.$$cloneForm(self.searchItem)
          postData.searchItem.level = 1
        };
        postData.getAfter = {
          user: {
            tableName: 'User',
            middleKey: 'user_no',
            key: 'user_no',
            condition: '=',
            searchItem: {
              status: 1
            },
            info: ['nickname'],
          }
        };
        postData.excelOutput = {
          expTitle: '商品订单',
          expCellName: [{
              title: '下单时间',
              key: ['create_time'],
              type: 'string',
            },{
              title: '订单编号',
              key: ['parent_no'],
              type: 'string',
            },
            {
              title: '订单金额',
              key: ['price'],
              type: 'string',
            },
            {
              title: '支付状态',
              key: ['pay_status'],
              type: 'string',
              formatter: {
                '-1': '已退款',
                0: '未支付',
                1: '已支付'
              }
            },
            {
              title: '商品名称',
              key: ['product_title'],
              type: 'string',
            },
            {
              title: '规格名称',
              key: ['sku_title'],
              type: 'string',
            },
            {
              title: '购买数量',
              key: ['count'],
              type: 'string',
            },
            {
              title: '单价',
              key: ['unit_price'],
              type: 'string',
            },
            {
              title: '收货地区',
              key: ['snap_address', 'address'],
              type: 'string',
            },
            {
              title: '详细地址',
              key: ['snap_address', 'detail'],
              type: 'string',
            },
            {
              title: '收件人姓名',
              key: ['snap_address', 'name'],
              type: 'string',
            },
            {
              title: '收件人电话',
              key: ['snap_address', 'phone'],
              type: 'string',
            },
            {
              title: '下单用户NO',
              key: ['user_no'],
              type: 'string',
            },
            {
              title: '用户昵称',
              key: ['user', 'nickname'],
              type: 'string',
            },
          ],
          fileName: 'test'
        };

        window.open('https://testtwo.solelyplus.com/680/public/index.php/api/v1/Common/Order/get?token=' +
          postData.token +
          '&searchItem=' +
          JSON.stringify(postData.searchItem) +
          '&getAfter=' +
          JSON.stringify(postData.getAfter) +
          '&excelOutput=' +
          JSON.stringify(postData.excelOutput))
      }

    },

    /**
     * 列表主函数
     */
    async initMainData(isNew) {

      const self = this;
      self.table_arguments.loading = true;
      const postData = {};
      if (isNew) {
        self.paginate.currentPage = 1;
      };
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      postData.getAfter = {
        user: {
          tableName: 'User',
          middleKey: 'user_no',
          key: 'user_no',
          condition: '=',
          searchItem: {
            status: 1
          },
          info: ['nickname'],
        },
        staff: {
          tableName: 'User',
          middleKey: 'passage1',
          key: 'user_no',
          condition: '=',
          searchItem: {
            status: 1
          },
          info: ['login_name','nickname','name'],
        }
      };

      var res = await self.$$api_orderGet({
        data: postData
      });
      self.mainData = res.info.data;
      self.paginate.count = res.info.total;
      self.table_arguments.loading = false;

    },

    /**
     * 列表主函数
     */
    async initLabel() {
      const self = this;
      const postData = {};
      postData.token = self.$store.getters.getToken;
      postData.searchItem = {
        type: 5
      };
      var res = await self.$$api_labelGet({
        data: postData
      });
      self.optionData.labelOptions = res.info.data;
    },

    async initExpress() {
      const self = this;
      const postData = {};
      postData.token = self.$store.getters.getToken;
      postData.searchItem = {
        menu_id: 32
      };
      var res = await self.$$api_articleGet({
        data: postData
      });
      self.optionData.expressOptions = res.info.data;
    },

    async initProductData(postData) {

      const self = this;
      postData.token = self.$store.getters.getToken;
      var res = await self.$$api_productGet({
        data: postData
      });

      if (res.info.data.length > 0) {
        for (var i = 0; i < res.info.data.length; i++) {
          res.info.data[i]['value'] = res.info.data[i]['title'];
          res.info.data[i]['product_str'] = res.info.data[i]['id'];
        };
        return res.info.data;
      } else {
        return [{
          "value": "无",
          "product_str": ""
        }];
      };

    },


  },


}
